import gevent
from gevent import monkey
monkey.patch_all()

from autoTask.Base import Login, ThrowError
from common.logger.log import log_
from request.remote import code as remote_code


class TaoBao(Login):

    def __init__(self, account, password):
        super().__init__(account, password)
        self.action_map = None

    def listen_init(self):
        action_map = {
            'https://login.taobao.com/newlogin/login.do?appName=taobao&fromSite=0&_bx-v=2.2.3': self.do_check,
            'https://login.taobao.com/member/logout.jhtml': self.logout_check,
            'wss://ws.mmstat.com/ws/': self.slider
        }
        return action_map

    def do_check(self, response):
        log_.debug(response)
        data = response['content']['data']
        if data.get('redirect') is True:
            log_.debug('登录成功')
        # if data['titleMsg'] == '账号名或登录密码不正确':
        #     log_.error(data['titleMsg'])
        # {'content': {'data': {'titleMsg': '账号名或登录密码不正确', 'resultCode': 100}, 'status': 0, 'success': True}, 'hasError': False}
        # {'content': {'data': {'redirect': True, 'redirectUrl': 'https://mai.taobao.com/seller_admin.htm', 'asyncUrls': ['https://pass.tmall.hk/add?lid=%E4%B8%AD%E5%B1%B1%E6%9D%B0%E7%89%B9%E7%94%B5%E5%99%A8%3A%E6%98%9F%E6%98%9F&cookie2=1ac7e3b99ff04f0daae358639fd6b057&cancelledSubSites=empty&sn=%E4%B8%AD%E5%B1%B1%E6%9D%B0%E7%89%B9%E7%94%B5%E5%99%A8%3A%E6%98%9F%E6%98%9F&_tb_token_=35be30e8bee43&uc1=cookie21=VFC%2FuZ9aj3yE;cookie14=Uoe9bjFGtK0gIQ%3D%3D&unb=2209378523893&sgcookie=E100F8KPtnza%2F8bmk%2FEZGoZA7kQ%2FUESxMM1BEJmt%2FPgKkeuTwriOn9XJ3QPh6cbI5agZvXNvN6eDJIqmqeJD7k75lYJMFcyEALITRYB8bnGT%2BwI%3D&t=7396da3aad3cba78909139f4cbcf6ce8&csg=3f966912&tmsc=1693382326995000&opi=113.73.112.238&pacc=t4mAnZfzO-Dcc6MDn0X_Bg==&target=https%3A%2F%2Flist.tmall.hk%2Fsearch_product.htm', 'https://pass.tmall.com/add?lid=%E4%B8%AD%E5%B1%B1%E6%9D%B0%E7%89%B9%E7%94%B5%E5%99%A8%3A%E6%98%9F%E6%98%9F&cookie2=1ac7e3b99ff04f0daae358639fd6b057&cancelledSubSites=empty&sn=%E4%B8%AD%E5%B1%B1%E6%9D%B0%E7%89%B9%E7%94%B5%E5%99%A8%3A%E6%98%9F%E6%98%9F&_tb_token_=35be30e8bee43&uc1=cookie21=VFC%2FuZ9aj3yE;cookie14=Uoe9bjFGtK0gIQ%3D%3D&unb=2209378523893&sgcookie=E100F8KPtnza%2F8bmk%2FEZGoZA7kQ%2FUESxMM1BEJmt%2FPgKkeuTwriOn9XJ3QPh6cbI5agZvXNvN6eDJIqmqeJD7k75lYJMFcyEALITRYB8bnGT%2BwI%3D&t=7396da3aad3cba78909139f4cbcf6ce8&csg=3f966912&tmsc=1693382326995000&opi=113.73.112.238&pacc=t4mAnZfzO-Dcc6MDn0X_Bg==&target=https%3A%2F%2Fwww.tmall.com'], 'resultCode': 100}, 'status': 0, 'success': True}, 'hasError': False}

    def login(self):
        page = self.page
        url = 'https://login.taobao.com/'
        page.get(url)

    def login_step(self):
        log_.info('执行登录操作')
        page, hand = self.page, self.hand
        page.get('https://login.taobao.com/')
        hand.fill(self.account, 'x://input[@id="fm-login-id"]')
        hand.fill(self.password, 'x://input[@id="fm-login-password"]')
        hand.click('x://button[@class="fm-button fm-submit password-login"]')

    def logout_step(self):
        page = self.page
        page.get('https://login.taobao.com/member/logout.jhtml')

    def logout_check(self, response):
        log_.debug(response)

    def phone_code(self):
        pass

    def phone_code_step(self):
        pass

    def slider(self):
        log_.debug('检测到滑块')
        page, hand = self.page, self.hand
        slipper_box = page('x://div[@id="nocaptcha"]')
        slipper = slipper_box.ele("#nc_1_n1z", timeout=0)
        x, y = slipper.locations.midpoint
        print(x, y)
        hand.drag_x_p2p({'x': x, 'y': y+10}, 260)

    def debug(self):
        self.action_map = self.listen_init()
        tasks = [gevent.spawn(_) for _ in [self.listen]]
        # tasks = [gevent.spawn(_) for _ in [self.listen, self.login_step]]
        gevent.joinall(tasks)

    def debug_listen(self):
        self.listener.show_msg = False
        listener = self.listener
        action_map = self.action_map
        listener.set_targets([_ for _ in action_map])
        listener.listen()
        for i in listener.steps():
            url = i.url
            print(url)
            # response = i.body
            # gevent.spawn(action_map[url], response)


if __name__ == '__main__':
    TaoBao('1234', '123456').debug_listen()
    # TaoBao('中山杰特电器:星星', 'yzt123456').debug()
    # TaoBao('1234', '1234').login_step()
